package com.metadata_system.common.db.mapper;

import com.metadata_system.common.db.entity.Metadata;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import tk.mybatis.mapper.common.Mapper;

public interface MetadataMapper extends Mapper<Metadata> {
    @Select("select last_insert_id()")
    int getLastInsertId();


    @Update("UPDATE md_metadata " +
            "SET view_total = (SELECT COUNT(*) FROM md_metadata_view WHERE metadata_id = #{id}), " +
            "node_total = (SELECT COUNT(*) FROM md_metadata_node WHERE metadata_id = #{id}), " +
            "edge_total = (SELECT COUNT(*) FROM md_metadata_edge WHERE metadata_id = #{id}), " +
            "view_arr = (SELECT JSON_ARRAYAGG( " +
            "    JSON_OBJECT( " +
            "        'id', id, " +
            "        'name', name, " +
            "        'shortName', short_name " +
            "    ) " +
            ") FROM md_metadata_view WHERE metadata_id = #{id}) " +
            "WHERE id = #{id}")
    void updateMetadataStats(@Param("id") Integer id);
}